import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class lloguer extends HttpServlet implements Runnable {

  public void init(ServletConfig config) throws ServletException {
    super.init(config);  // siempre
  }

  public void run() {
  }

  public void destroy() {
  }

  public void doGet(HttpServletRequest req, HttpServletResponse res)
                    throws ServletException, IOException {
                    
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    
    // Afegir nou lloguer
    String carmodel = req.getParameter("model_vehicle");
    String submodel = req.getParameter("sub_model_vehicle");
    String duration = req.getParameter("dies_lloguer");
    String quantity = req.getParameter("num_vehicles");
    String discount = req.getParameter("descompte");
    
    if (carmodel == null || submodel == null || duration == null || quantity == null || discount == null) {
        out.println("<html><h1>Falten arguments, no s'ha fet res</h1></html>");
    } else {
    
        // Llistat de preus improvisat
        Map <String, Double> priceInfo = new HashMap<String, Double>();
        priceInfo.put("54", 200.0);
        priceInfo.put("71", 400.0);
        priceInfo.put("82", 699.95);
        priceInfo.put("139", 1234.5);
        
        // Calcular preu
        Double price = priceInfo.get(carmodel);
        price = price == null ? 299.95 : price; // Si no esta al llistat s'assigna el preu per defecte
        price *= submodel.equals("Gasolina") ? 1.1 : 0.9;
        price *= Integer.parseInt(duration);
        price *= Integer.parseInt(quantity);
        price -= price * (Double.parseDouble(discount) / 100);
        
        // Guardar a la 'base de dades'
        BufferedWriter fout = new BufferedWriter(new FileWriter("lloguers.txt", true));
        fout.write(carmodel + " " + submodel + " " + duration + " " + quantity + " " + discount + " " + price );
        fout.newLine();
        fout.close();
        
        // Mostrar resultat
        out.println("<html><h1>Nou lloguer afegit</h1>");
        out.println("Codi model: " + carmodel + "<br />");
        out.println("Motor: " + submodel + "<br />");
        out.println("Nombre dies: " + duration + "<br />");
        out.println("Nombre vehicles: " + quantity + "<br />");
        out.println("Descompte: " + discount + "<br />");
        out.println("<strong>Preu final: " + price + "</strong><br />");
        out.println("<a href=\"../main.html\">Tornar a la plana principal</a></html");
    }
  }

  public void doPost(HttpServletRequest req, HttpServletResponse res)
                    throws ServletException, IOException {
                    
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    
    // Llistar lloguers
    String username = req.getParameter("userid");
    String password = req.getParameter("password");
    out.println("<html>");
    
    // 'base de dades' de models
    Map <String, String> modelInfo = new HashMap<String, String>();
    modelInfo.put("54", "Economic");
    modelInfo.put("71", "Semi-Luxe");
    modelInfo.put("82", "Luxe");
    modelInfo.put("139", "Limusina");
    
    if (username != null && username.equals("Admin") &&
        password != null && password.equals("password")) {
    	//Login ok
        out.println("<h1> Bienvenido " + username + "</h1>"); 
        DataInputStream in = new DataInputStream(new FileInputStream("lloguers.txt"));
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine = br.readLine();
        out.println("<ol>");
        while (strLine != null) {
        	String[] words = strLine.split(" ");
        	out.println(
        			"<li>" +
        			"Model: " + modelInfo.get(words[0]) + ", &nbsp" +
        			"Motor: " + words[1] + ", &nbsp" +
        			"Dies: " + words[2] + ", &nbsp" +
        			"Quantitat: " + words[3] + ", &nbsp" +
        			"Descompte: " + words[4] + ", &nbsp" + 
        			"Preu Final: " + words[5] +
        			"</li>"
        			);
            strLine = br.readLine();
        }
        out.println("</ol>");
    } else {
    	//Login fail
        out.println("<h1>Wrong login</h1>");
    }
    out.println("<a href=\"../main.html\">Tornar a la plana principal</a></html");
  }
}
